package com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.script_library;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.configs.BasicConfig;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ScriptLibraryAdp;
import com.baihan.angus.app.game.busshow.databinding.ActivityScriptLibraryBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.ScriptResponse;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.Map;

/**
 * 剧本库
 */
@Route(path = ArouterAppPath.SCRIPT_LIBRARY_ACTIVITY)
public class ScriptLibraryActivity extends BaseActivity<ActivityScriptLibraryBinding, ScriptLibraryVM> {

    private ScriptLibraryAdp mScriptLibraryAdp;

    private Userinfo userInfo;

    private Map<String, Object> mParameter = new HashMap<>();
    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_script_library;
    }

    @Override
    protected int getViewModelId() {
        return BR.scriptLibraryVM;
    }

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(BasicConfig.getInstance().isDayMode()).init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        LinearLayout.LayoutParams llTitle = (LinearLayout.LayoutParams) mVDBinding.llTitleView.getLayoutParams();
        llTitle.topMargin = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.llTitleView.setLayoutParams(llTitle);

        userInfo = UserConfig.getUserConfig().getUser();

        mVDBinding.ivBack.setOnClickListener(v -> {
            if (mVDBinding.flSearch.getVisibility() == View.VISIBLE) {
                mVDBinding.flSearch.setVisibility(View.GONE);
                mVDBinding.flTitle.setVisibility(View.VISIBLE);
            } else {
                finish();
            }
        });

        mVDBinding.ivSearch.setOnClickListener(v -> {
            mVDBinding.flSearch.setVisibility(View.VISIBLE);
            mVDBinding.flTitle.setVisibility(View.GONE);
        });

        mVDBinding.flSearch.setOnClickListener(v -> {

        });

        mVDBinding.ivClearWord.setOnClickListener(v -> {
            mViewModel.ofEtWord.set("");
        });

        mScriptLibraryAdp = new ScriptLibraryAdp();
        mVDBinding.rvScript.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mScriptLibraryAdp.bindToRecycleView(mVDBinding.rvScript);

        mVDBinding.srlScript.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mParameter.put("page", mPage);
            mViewModel.getScriptList(mParameter);
        });

        mVDBinding.srlScript.setOnLoadMoreListener(refreshLayout -> {
            mParameter.put("page", ++mPage);
            mViewModel.getScriptList(mParameter);
        });

        mScriptLibraryAdp.setOnItemClickListener(new ScriptLibraryAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("scriptItem", mScriptLibraryAdp.getItem(position));
                intent.putExtras(bundle);
//                intent.putExtra("scriptId", mScriptLibraryAdp.getItem(position).getScript_id());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        mParameter.put("page", mPage);
        mParameter.put("size", mSize);
        mParameter.put("type", "room");
        mViewModel.getScriptList(mParameter);
    }

    @Override
    protected void initObservers() {
        mViewModel.mldScript.observe(this, new Observer<ScriptResponse.DataX>() {
            @Override
            public void onChanged(ScriptResponse.DataX dataX) {
                if (mVDBinding.srlScript.isRefreshing()) mVDBinding.srlScript.finishRefresh();
                if (mVDBinding.srlScript.isLoading()) mVDBinding.srlScript.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mScriptLibraryAdp.setNewData(dataX.getData());
                } else {
                    mScriptLibraryAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlScript.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlScript.setEnableLoadMore(false);
                }
            }
        });
    }
}